package top.lyc829.mymusic.ui.custom

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lxj.xpopup.core.BottomPopupView
import com.lzx.starrysky.StarrySky
import com.lzx.starrysky.control.RepeatMode
import top.lyc829.mymusic.MyApp
import top.lyc829.mymusic.R
import top.lyc829.mymusic.adapter.MusicListDialogAdapter
import top.lyc829.mymusic.databinding.CustomMusicListDialogBinding
import top.lyc829.mymusic.viewmodel.EventViewModel
import com.lxj.xpopup.util.XPopupUtils




/**
 * @Author lyc
 * @create 2021-07-25 23:44
 */
class MusicListDialog(context: Context) : BottomPopupView(context) {

    companion object {
        const val TAG = "MusicListDialog"
    }
    private val eventViewModel: EventViewModel by lazy {
        ViewModelProvider(MyApp.context as MyApp).get(EventViewModel::class.java)
    }
    private val adapter: MusicListDialogAdapter by lazy { MusicListDialogAdapter(context) }

    private lateinit var tvTotalNumber: TextView
    private lateinit var tvMode: TextView
    private lateinit var imgMode: ImageView
    private lateinit var recyclerView: RecyclerView

    override fun getImplLayoutId(): Int {
        return R.layout.custom_music_list_dialog
    }

    override fun onCreate() {
        super.onCreate()

        initView()
        clickListener()
        observer()
        initData()
    }



    private fun initView() {

        tvTotalNumber = findViewById(R.id.num_text_view)
        tvMode = findViewById(R.id.mode_text_view)
        imgMode = findViewById(R.id.mode_image_view)
        recyclerView = findViewById(R.id.recycler)

        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.adapter = adapter
    }

    private fun clickListener() {
        adapter.setOnItemClickListener { _, _, position ->
            StarrySky.with().playMusicById(adapter.getItem(position).songId)
        }

        tvMode.setOnClickListener {
            eventViewModel.updateRepeatMode()
        }
    }

    private fun observer() {
        eventViewModel.playList.observe(context as LifecycleOwner) {
            tvTotalNumber.text = "(${it.size})"
            adapter.setList(it)
        }

        eventViewModel.playBackStage.observe(context as LifecycleOwner) { state ->
            var needReturn = 0
            adapter.data.forEachIndexed { index, songInfo ->
                if (songInfo.songId == state.songInfo?.songId) {
                    adapter.updateCurrentSongInfo(songInfo, index)
                    needReturn++
                }
                if (songInfo.songId == state.lastSongInfo?.songId) {
                    adapter.updateCurrentSongInfo(state.lastSongInfo!!, index)
                    needReturn++
                }
                if (needReturn == 2) return@forEachIndexed
            }
        }

        eventViewModel.repeatMode.observe(context as LifecycleOwner) {
            tvMode.text = when (it.repeatMode) {
                RepeatMode.REPEAT_MODE_NONE -> {
                    imgMode.setImageResource(R.mipmap.loop)
                    "顺序播放"
                }
                RepeatMode.REPEAT_MODE_ONE -> {
                    imgMode.setImageResource(R.mipmap.once)
                    "单曲播放"
                }
                RepeatMode.REPEAT_MODE_SHUFFLE -> {
                    imgMode.setImageResource(R.mipmap.random)
                    "随机播放"
                }
                RepeatMode.REPEAT_MODE_REVERSE -> {
                    imgMode.setImageResource(R.mipmap.loop)
                    "倒序播放"
                }
                else -> {
                    imgMode.setImageResource(R.mipmap.loop)
                    "未知"
                }
            }
        }
    }

    private fun initData() {

    }

    override fun getMaxHeight(): Int {
        return (XPopupUtils.getAppHeight(context)*.85f).toInt()
    }

}